<template>
	<ex-dialog v-model="visible" draggable :title="selectorName" :width="width" footershow="none">
		<template #default>
			<el-container>
				<el-aside class="nopadding dialogsearch" style="width: 300px;margin-left: 0px;">
					<search-form v-if="searches" :searches="searches" :search-form="searchForm"></search-form>
					<div style="flex: auto;text-align: center;padding-left: 40px;margin-top: 15px;">
						<el-button plain type="primary" icon="el-icon-search" @click="searchHandler" :loading="loading">
							{{ $t('button.search_ok') }}
						</el-button>
						<el-button plain type="warning" icon="el-icon-brush" @click="resetHandler">
							{{ $t('button.search_clear') }}
						</el-button>
					</div>
				</el-aside>
				<el-main class="nopadding" style="margin-bottom: 10px;">
					<div style="height: 10px;"></div>
					<ex-table id="assetTable" ref="selecterTable" :columns="columns" :height="550" size="small"
								:url="url" :params="form" :refs="this.$refs" @columnClick="columnClickHandler"
								selectionType="radio"></ex-table>
					<div style="flex: auto;text-align: center;margin-top: 5px;margin-bottom: 10px;">
						<el-button plain type="primary" @click="save" :loading="loading">确 定</el-button>
						<el-button plain type="info" @click="this.visible = false" :loading="loading">取 消</el-button>
					</div>
				</el-main>
			</el-container>
		</template>
	</ex-dialog>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import entity from "@/constant/model/entity";
import exDialog from '@/components/exDialog'
import {defineAsyncComponent} from "vue";

export default {
	name: "selector",
	computed: {},
	emits: ['success'],
	components: {
		SearchForm:defineAsyncComponent(() => import('@/components/exSearch/search_form.vue')),
		ExTable,
		exDialog,
	},
	props: {
		ids: {type: Array, default: []},
		width: {type: Number, default:1300},
    data: {type:Object,default: null}
	},
	data() {
		return {
			visible: false,
			form: {},
			model: {},
			loading: false,
			oldForm: null,
			searchForm: {},
			searches: null,
			columns: [],
			url: null,
			selectorName: null,
		}
	},
	watch: {},
	mounted() {
	},
	onLoad() {
	},
	methods: {
		initData() {
			let visible = true;
			if (this.form.selector === entity.FamsCostCenter.name) {
				this.searches = [
					{type: "input", name: "编码", code: "costCenterCode"},
					{type: "input", name: "名称", code: "costCenterName"},
					{type: "input", name: "财务编码", code: "other.cwCode"},
				];
				this.columns = [
					{code: "costCenterCode", name: "编码", width: "180"},
					{code: "costCenterName", name: "名称", width: ""},
					{code: "other.cwCode", name: "财务编码", width: "100"},
					{code: "other.type", name: "类型", width: "60"},
				];
				this.url = this.$API.system.costcenter.list.url;
				this.selectorName = "成本中心";
			}else if (this.form.selector === entity.SysHelpCenter.name) {
				this.searches = [
					{type: "input", name: "名称", code: "name"},
				];
				this.columns = [
					{code: "name", name: "名称", width: ""},
					{code: "type", name: "类型", width: "100", sortable: true, formatter: "DICT", key: "HELPTYPE"},
					{code: "isEnabled", name: "是否可用", width: "100", sortable: true, formatter: "DICT", key: "IS_ENABLED"},
					{code: "createUser", name: "创建人", width: "100", formatter: "DICT", key: "SysUser"},
					{code: "createTime", name: "创建时间", width: "175", sortable: true, formatter: "DATETIME"},
				];
				this.url = this.$API.system.help.list.url;
				this.selectorName = "帮助中心";
			} else if (this.form.selector === entity.SysUser.name) {
				this.searches = [
					{type: "input", name: "员工名称", code: "userName"},
					{type: "input", name: "员工编号", code: "userCode"},
				];
				if(this.form._preRelationProp){
					this.form.orgId = this.form._preRelationProp;
				}else{
					this.searches.push({type: "tree-select", name: "所在部门", code: "orgId",dictCode:"SysOrganization",priKey:true});
				}
				this.columns = [
					{code: "orgId", name: "所在部门", width: "",formatter:"DICT",key:"SysOrganization"},
					{code: "userName", name: "员工名称", width: ""},
					{code: "loginName", name: "登录名", width: "150"},
					{code: "userCode", name: "员工编号", width: "100"},
					{code: "status", name: "状态", width: "60",formatter: "DICT",key:"USER_STATUS"},
          {code: "employee.employeeStatus", name: "员工状态", width: "100",formatter:"DICT",key:"EMPLOYEE_STATUS"},
				];
				this.url = this.$API.system.operator.list.url;
				this.selectorName = "人员";
			}else if (this.form.selector === entity.FamsProvider.name) {
				this.searches = [
					{type: "input", name: "编码", code: "providerCode"},
					{type: "input", name: "名称", code: "providerName"},
					{type: "input", name: "联系人", code: "contactName"},
					{type: "input", name: "联系电话", code: "contactPhone"},
				];
				this.columns = [
					{code: "providerCode", name: "编码", width: "200"},
					{code: "providerName", name: "名称", width: ""},
				];
				this.url = this.$API.system.provider.list.url;
        this.form.relDeptId = this.$TOOL.current.user().orgId;//查询当前登录人所在部门的承担成本中心的供应商信息
				this.selectorName = "供应商";
			}else if (this.form.selector === entity.SysOrganization.name) {
				this.searches = [
					{type: "input", name: "部门名称", code: "orgName"},
					{type: "input", name: "部门编号", code: "orgCode"},
				];
				this.columns = [
					{code: "orgCode", name: "部门编号", width: "200"},
					{code: "orgName", name: "部门名称", width: ""},
				];
				this.url = this.$API.system.dept.list.url;
				this.selectorName = "部门";
			}else {
				this.$message.error("未找到适配的选择器");
				visible = false;
			}
			this.visible = visible;
		},
		//显示
		async open(entity) {
			this.searches = null;
			this.columns = [];
			this.form = Object.assign({}, entity);
			this.oldForm = this.$TOOL.objCopy(this.oldForm);
			this.resetHandler();
			this.initData();
		},
		async close(){
			this.visible = false;
		},
		async searchHandler() {
			await this.$refs.selecterTable.refresh(this.searchForm);
		},
		resetHandler() {
			this.searchForm = {};
		},
		columnClickHandler(name, data) {
			this.$options.methods[name](this, data);
		},
		//保存
		async save() {
			let selections = this.$refs.selecterTable.selections();
			if (selections.length === 0) {
				this.$message.warning(this.$t("common.please_select_data"));
				return;
			}

			let confirm = await this.$confirm('请确认是否提交选择的数据', '提示', {type: 'warning'}).then(() => {
				return true;
			}).catch(() => {
				return false;
			});
			if (!confirm) return;
			selections.forEach(row => {
				this.ids[0] = row.id;
        		Object.assign(this.data,row);
			});
			this.$emit('success');
			this.visible = false;
		},
	}
}
</script>

<style scoped>
h2 {
  font-size: 17px;
  color: #3c4a54;
  padding: 0 0 30px 0;
}

[data-theme="dark"] h2 {
  color: #fff;
}

::v-deep(.dialogselect .el-dialog__header){
  padding: 0px !important;
  margin-right: 0px;
}
::v-deep(.dialogsearch){
 padding-top: 10px;
}

::v-deep(.dialogsearch .el-form-item--default .el-form-item__label){
  font-size: 13px !important;
  color: rgb(101, 101, 101) !important;
}

::v-deep(.dialogsearch .el-form-item--default .el-input__inner,.dialogsearch .el-form-item--default .el-date-editor){
  font-size: 13px !important;
}

</style>
